﻿using BrnMall.Manager.Domain.Store;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.Store
{
    public static class BranchManager
    {
        public static bool Add(BranchInfo model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
                return model.Bid > 0;
            }
        }

        public static bool Update(BranchInfo model)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Update(model);

            }
            return count > 0;
        }

        public static bool UpdateImgs(int bid, string imgs)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute("update bma_Branch set imgs=@0 where bid=@1", imgs, bid);
            }
            return count > 0;
        }

        public static bool Delete(int bid, int storeId)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute(string.Format("delete bma_Branch where bid={0} and storeId={1}", bid, storeId));
            }
            return count > 1;
        }

        public static BranchInfo Get(int bid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<BranchInfo>(string.Format("select * from bma_Branch where bid={0}", bid));
            }
        }

        public static List<BranchInfo> GetList(int storeId, int pageSize)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<BranchInfo>(string.Format("select top {0} bid,storeid,name,logo,phone,address,location,createtime from bma_Branch where storeid={1} order by  bid desc", pageSize, storeId));
            }
        }

        public static Page<BranchInfo> GetListPage(int storeId, int pageNumber, int pageSize = 15)
        {
            var sql = string.Format("select bid,storeid,name,logo,phone,address,location,displayorder,createtime from bma_Branch where storeid={0} order by bid desc", storeId);
            using (var db = new QDMallDatabase())
            {
                return db.Page<BranchInfo>(pageNumber, pageSize, sql);
            }
        }
    }
}